package com.bong.ring.activity;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.ArrayMap;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.bong.ring.R;
import com.bong.ring.adapter.DeviceListAdapter;

import java.util.List;

@TargetApi(21)
public class DeviceListActivity extends AppCompatActivity {

    DeviceListAdapter deviceListAdapter;
    ListView deviceList;
    ArrayMap<String, Integer> deviceMap = new ArrayMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("附近的bong");
        deviceListAdapter = new DeviceListAdapter(this);
        deviceList = findViewById(R.id.list_device);
        deviceList.setAdapter(deviceListAdapter);
        deviceListAdapter.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(DeviceListActivity.this, DeviceConfirmActivity.class));
            }
        });
        initBle();
        beginScan();
    }

    private DeviceListAdapter adapter;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothManager bluetoothManager;
    private ScanCallback scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            BluetoothDevice device = result.getDevice();
            String name = device.getName();
            String addr = device.getAddress();
            if (!deviceMap.containsKey(device.getAddress())) {
                deviceMap.put(addr, result.getRssi());
                String[] data = new String[]{name, addr};
                deviceListAdapter.add(data);
                deviceListAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
        }
    };

    private void initBle() {
        // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
        // BluetoothAdapter through BluetoothManager.
        bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        // Checks if Bluetooth is supported on the device.
        if (mBluetoothAdapter == null) {
            finish();
            return;
        }
        mBluetoothAdapter.enable();
    }

    Runnable stopScan = new Runnable() {
        @Override
        public void run() {

            mBluetoothAdapter.getBluetoothLeScanner().stopScan(scanCallback);
        }
    };
    Handler handler = new Handler();

    public void beginScan() {
        List<ScanFilter> scanFilters = null;
        ScanSettings scanSettings = null;
        if (scanFilters != null && scanSettings != null)
            mBluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings, scanCallback);
        else
            mBluetoothAdapter.getBluetoothLeScanner().startScan(scanCallback);
        handler.postDelayed(stopScan, 60000);//60s后停止扫描
    }

    public void stopScan() {
        handler.removeCallbacks(stopScan);
        mBluetoothAdapter.getBluetoothLeScanner().stopScan(scanCallback);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        stopScan();
        super.onDestroy();
    }
}
